﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using DBUtility;
using IDAL;
using Model;

namespace DAL
{
    public class Repair : BaseRepository_DT<RepairInfo>, IRepair
    {
        #region 静态变量定义部分
        private const string PARM_REPAIR_PRODUCTSN = "@ProductSN";
        private const string PARM_REPAIR_RECORDTIME = "@RecordTime";
        private const string PARM_REPAIR_USERID = "@UserID";
        private const string PARM_REPAIR_REMARK = "@Remark";

        private const string PARM_REPAIR_SNITEMDT = "@RepairSNItem";
        private const string PARM_REPAIR_STEPITEMDT = "@RepairStepItem";

        private const string PARM_REPAIR_PACKAGEPRODUCTSN = "@PackageProductSN";

        private const string PARM_REPAIR_REPAIRPATH = "@RepairPath";
        #endregion


        #region 操作部分
        public bool RepairItem_Insert(DateTime recordTime,string userID,string remark, DataTable sNdt,DataTable stepdt)
        {
            List<SqlParameter> sNList = new List<SqlParameter>() {
            sqlHelper.CreateSqlParemeterStructured(PARM_REPAIR_SNITEMDT,sNdt)};
            List<SqlParameter> stepList = new List<SqlParameter>() {
            sqlHelper.CreateSqlParemeterStructured(PARM_REPAIR_STEPITEMDT,stepdt)};
            SqlParameter[] Parms = GetDTUIDParameters();
            Parms[0].Value = recordTime;
            Parms[1].Value = userID;
            Parms[2].Value = remark;

            SqlCommand cmd = new SqlCommand();
            foreach (SqlParameter parm in Parms)
                cmd.Parameters.Add(parm);
            foreach (SqlParameter parm in sNList)
                cmd.Parameters.Add(parm);
            foreach (SqlParameter parm in stepList)
                cmd.Parameters.Add(parm);

            using (SqlConnection conn = new SqlConnection(sqlHelper.ConnectionStringLocalTransaction))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                try
                {
                    cmd.CommandText = "RepairItem_Insert";
                    using (SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { }
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        public bool Repair_AutoBuild(string productSN, string repairPath, string userID)
        {
            SqlParameter[] parms = GetSNPathUIDParameters();
            parms[0].Value = productSN;
            parms[1].Value = repairPath;
            parms[2].Value = userID;

            SqlCommand cmd = new SqlCommand();
            foreach (SqlParameter parm in parms)
                cmd.Parameters.Add(parm);

            using (SqlConnection conn = new SqlConnection(sqlHelper.ConnectionStringLocalTransaction))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                try
                {
                    cmd.CommandText = "RepairItem_AutoInsert";
                    using (SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { }
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }
        #endregion


        #region 检索部分
        public RepairInfo GetRepairbyLast(string productSN)
        {
            SqlParameter parm = GetParameter();
            parm.Value = productSN;
            return ExcuteSqlToGetEntity("Repair_selectbyLastSN", parm);
        }

        public RepairInfo GetRepairbySNDT(string productSN, DateTime recordTime)
        {
            SqlParameter[] parms = GetSNDTParameter();
            parms[0].Value = productSN;
            parms[1].Value = recordTime;
            return ExcuteSqlToGetEntity("Repair_selectbySNDT", parms);
        }

        public IList<RepairInfo> GetRepairbySN(string productSN)
        {
            SqlParameter parm = GetParameter();
            parm.Value = productSN;
            return ExcuteSqlToGetEntities("Repair_selectbySN", parm);
        }

        public IList<RepairInfo> GetRepairbyPackageSN(string packageSN)
        {
            SqlParameter parm = new SqlParameter(PARM_REPAIR_PACKAGEPRODUCTSN, SqlDbType.NVarChar, 50);
            parm.Value = packageSN;
            return ExcuteSqlToGetEntities("Repair_selectbyPackageSN", parm);
        }

        public IList<RepairInfo> GetRepairbyDT(DateTime dt1, DateTime dt2)
        {
            SqlParameter[] parms = GetDTParameters();
            parms[0].Value = dt1;
            parms[1].Value = dt2;
            return ExcuteSqlToGetEntities("Repair_selectbyDT", parms);
        }
        #endregion

        #region 参数部分
        public SqlParameter[] GetSNDTParameter()
        {
            SqlParameter[] parms = null;
            parms = new SqlParameter[]
                {
                    new SqlParameter(PARM_REPAIR_PRODUCTSN, SqlDbType.NVarChar, 50),
					new SqlParameter(PARM_REPAIR_RECORDTIME, SqlDbType.DateTime, 8),
                };
            return parms;
        }

        public SqlParameter[] GetDTUIDParameters()
        {
            SqlParameter[] parms = null;
            parms = new SqlParameter[]
                {
					new SqlParameter(PARM_REPAIR_RECORDTIME, SqlDbType.DateTime, 8),
                    new SqlParameter(PARM_REPAIR_USERID, SqlDbType.NVarChar, 10),
                    new SqlParameter(PARM_REPAIR_REMARK, SqlDbType.NVarChar, 200),
                };
            return parms;
        }

        public SqlParameter[] GetSNPathUIDParameters()
        {
            SqlParameter[] parms = null;
            parms = new SqlParameter[]
                {
					new SqlParameter(PARM_REPAIR_PRODUCTSN, SqlDbType.NVarChar, 50),
                    new SqlParameter(PARM_REPAIR_REPAIRPATH, SqlDbType.NVarChar, 50),
                    new SqlParameter(PARM_REPAIR_USERID, SqlDbType.NVarChar, 10),
                };
            return parms;
        }

        #endregion

        #region 重写基类部分
        public override RepairInfo GetEntityRecord(SqlDataReader rdr)
        {
            RepairInfo pro = new RepairInfo { ProductSN = rdr.GetValue(0).ToString(), PackageProductSN = rdr.GetValue(1).ToString(), RecordTime = rdr.GetDateTime(2), UserID = rdr.GetValue(3).ToString(),Remark=rdr.GetValue(4).ToString() };
            return pro;
        }

        public override RepairInfo GetEntityRecord()
        {
            RepairInfo pro = new RepairInfo();
            return pro;
        }

        public override SqlParameter GetParameter()
        {
            SqlParameter Parm = new SqlParameter(PARM_REPAIR_PRODUCTSN, SqlDbType.NVarChar, 50);
            return Parm;
        }
        #endregion
    }
}
